package com.example.foo.admin.web.product;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.product.model.Bag;
import com.example.foo.product.query.BagQuery;
import com.example.foo.product.service.BagService;

@org.springframework.web.bind.annotation.RestController
public class BagAction {

	private final BagService bagService;

	public BagAction(BagService bagService) {
		this.bagService = bagService;
	}

	@GetMapping("/api/bags")
	public AjaxResult index(BagQuery bagQuery, Pageable pageable) {
		PageResult<Bag> pageResult = bagService.findBags(bagQuery, pageable);
		return AjaxResult.successData(pageResult);
	}

	@GetMapping("/api/bags/{id}")
	public AjaxResult showBag(@PathVariable("id") Long id) {
		Bag bag = bagService.findBagById(id);
		if (bag == null) {
			return AjaxResult.error(404, "Not Found");
		}
		return AjaxResult.successData(bag);
	}

	@PostMapping("/api/bags")
	public AjaxResult createBag(Bag bagForm) {
		AjaxResult validateError = validateForm(bagForm);
		if (validateError != null) {
			return validateError;
		}

		Bag bag = new Bag();
		bag.setName(bagForm.getName());
		bag.setPicUrl(bagForm.getPicUrl());
		bag.setSlogan(bagForm.getSlogan());
		bag.setGiftPoint(bagForm.getGiftPoint());
		bag.setGiftMoney(bagForm.getGiftMoney());
		bag.setItemId(bagForm.getItemId());
		bag.setGiftItemIds(bagForm.getGiftItemIds());
		bag.setGiftItemQuota(bagForm.getGiftItemQuota());
		bagService.createBag(bag);

		return AjaxResult.success();
	}

	@PutMapping("/api/bags/{id}")
	public AjaxResult updateBag(@PathVariable("id") Long id, Bag bagForm) {
		AjaxResult validateError = validateForm(bagForm);
		if (validateError != null) {
			return validateError;
		}

		Bag bag = bagService.findBagById(id);
		if (bag == null) {
			return AjaxResult.error(404, "Not Found");
		}

		bag.setName(bagForm.getName());
		bag.setPicUrl(bagForm.getPicUrl());
		bag.setSlogan(bagForm.getSlogan());
		bag.setGiftPoint(bagForm.getGiftPoint());
		bag.setGiftMoney(bagForm.getGiftMoney());
		bag.setItemId(bagForm.getItemId());
		bag.setGiftItemIds(bagForm.getGiftItemIds());
		bag.setGiftItemQuota(bagForm.getGiftItemQuota());
		bagService.updateBag(bag);

		return AjaxResult.success();
	}

	@PutMapping("/api/bags/{id}/state")
	public AjaxResult updateBagState(@PathVariable("id") Long id, @RequestParam("state") Bag.State state) {
		if (state == null) {
			return AjaxResult.error(400, "state不能为空");
		}

		Bag bag = bagService.findBagById(id);
		if (bag == null) {
			return AjaxResult.error(404, "Not Found");
		}

		bagService.updateBagState(id, state);

		return AjaxResult.success();
	}

	private AjaxResult validateForm(Bag bagForm) {
		if (StringUtils.isBlank(bagForm.getName())) {
			return AjaxResult.error(400, "名称不能为空");
		}
		if (StringUtils.isBlank(bagForm.getPicUrl())) {
			return AjaxResult.error(400, "主图不能为空");
		}
		if (StringUtils.isBlank(bagForm.getSlogan())) {
			return AjaxResult.error(400, "标语不能为空");
		}
		if (bagForm.getGiftPoint() == null) {
			return AjaxResult.error(400, "赠送积分不能为空");
		}
		if (bagForm.getGiftMoney() == null) {
			return AjaxResult.error(400, "赠送金额不能为空");
		}
		if (bagForm.getItemId() == null) {
			return AjaxResult.error(400, "商品ID不能为空");
		}
		if (StringUtils.isBlank(bagForm.getGiftItemIds())) {
			return AjaxResult.error(400, "礼品商品IDs不能为空");
		}
		if (bagForm.getGiftItemQuota() == null) {
			return AjaxResult.error(400, "礼品商品限额不能为空");
		}
		return null;
	}

}
